package com.vogue.supplymall.shop.controller;

import com.vogue.supplymall.common.constant.ConstantBean;
import com.vogue.supplymall.common.domain.dto.SearchInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.common.domain.tool.VestUtils;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.shop.common.CheckSearchInfoUtils;
import com.vogue.supplymall.shop.domain.model.ProductDetails;
import com.vogue.supplymall.shop.domain.model.ProductImg;
import com.vogue.supplymall.shop.domain.service.ProductDetailsService;
import com.vogue.supplymall.shop.domain.service.ProductImgService;
import com.vogue.supplymall.shop.domain.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * The type Product controller.
 * 提供店铺信息的controller
 *
 * @Aouthor: zhangliang
 * @Date: Create in 2017-12-14
 */
@RestController
public class ProductController extends BaseController {
    
    public static final Logger LOGGER_PRODUCTCONTROLLER = LoggerFactory.getLogger(ProductController.class);

    @Resource
    private ProductService productService;
    @Resource
    private ProductImgService productImgService;
    @Resource
    private ProductDetailsService productDetailsService;
    @Autowired
    private MessageSourceUtils messageSourceUtils;
    /**
     * Gets product by id.
     * 商品详情：根据id检索商品
     *
     * @param appver the appver
     * @param id     the id
     * @return the product by id
     */
    @RequestMapping(value = "/ns/{appver}/product/get/{id}", method = RequestMethod.POST)
    public ResponseEntity<List> getProductById(@PathVariable("appver") String appver, @PathVariable("id") Long id) {
        UserInfo info = new UserInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            LOGGER_PRODUCTCONTROLLER.info("登录人：" + info.getLoginId());
        } else {
            info.setSyscodeid(-1L);
            LOGGER_PRODUCTCONTROLLER.info("未登录");
        }
        List<Map<String, Object>> product = productService.findProductById(id, info);
        return new ResponseEntity<>(product, HttpStatus.OK);
    }

    /**
     * Gets product by id.
     * 商品详情：根据id检索商品图片
     *
     * @param appver the appver
     * @param id     the id
     * @return the product by id
     */
    @RequestMapping(value = "/ns/{appver}/product/getimg/{id}", method = RequestMethod.POST)
    public ResponseEntity<List> getProductImgById(@PathVariable("appver") String appver, @PathVariable("id") Long id) {
        List<ProductImg> productImg = productImgService.findByProductId(id);
        return new ResponseEntity<>(productImg, HttpStatus.OK);
    }

    /**
     * Gets product detail.
     * 获取商品详情的
     *
     * @param appver the appver
     * @param id     the id
     * @return the product detail
     */
    @RequestMapping(value = "/ns/{appver}/product/getDetail/{id}", method = RequestMethod.POST)
    public ResponseEntity<Object> getProductDetail(@PathVariable("appver") String appver, @PathVariable("id") Long id) {
        List<ProductDetails> productDetails = productDetailsService.findProductDetail(1, id);
        return new ResponseEntity<>(productDetails, HttpStatus.OK);
    }

    /**
     * Gets product detail img.
     *
     * @param appver the appver
     * @param id     the id
     * @return the product detail img
     */
    @RequestMapping(value = "/ns/{appver}/product/getDetailImg/{id}", method = RequestMethod.POST)
    public ResponseEntity<Object> getProductDetailImg(@PathVariable("appver") String appver, @PathVariable("id") Long id) {
        List<ProductDetails> productDetails = productDetailsService.findProductDetail(2, id);
        return new ResponseEntity<>(productDetails, HttpStatus.OK);
    }

    /**
     * Search product response entity.
     * 根据条件检索商品信息
     *
     * @param appver     the appver
     * @param searchInfo the search info
     * @param request    the request
     * @return the response entity
     */
    @RequestMapping(value = "/ns/{appver}/product/list", method = RequestMethod.POST)
    public ResponseEntity<Object> searchProduct(@PathVariable("appver") String appver,
                                                @RequestBody SearchInfo searchInfo, HttpServletRequest request) {
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            searchInfo.setSyscodeid(info.getSyscodeid());
            LOGGER_PRODUCTCONTROLLER.info("登录人：" + info.getLoginId());
        } else {
            LOGGER_PRODUCTCONTROLLER.info("未登录");
        }

        if (CheckSearchInfoUtils.apply(searchInfo)) {
            LOGGER_PRODUCTCONTROLLER.info("parameter check failed!");
            throw new DataNotFindException(
                    messageSourceUtils.getMessage(
                            com.vogue.supplymall.common.constant.ConstantBean.GLOBAL_SYSTEM + "product", null));
        }
        if (StringUtils.isEmpty(searchInfo.getPosition())) {
            searchInfo.setPosition(ConstantBean.AREA_CITY);
            searchInfo.getPositions().put("8", ConstantBean.AREA_CITY);
        }

        try {
            Object result = null;
            if (VestUtils.isVisitFromPC()) {
                //PC端 分页写法
                result = productService.findListBySearchInfo(searchInfo);
            } else {
                //App 滚动下拉
                result = productService.findBySearchInfo(searchInfo);
                LOGGER_PRODUCTCONTROLLER.info("sessionid:" + searchInfo.getSessionId() + ", result.size:" + ((List<Map<String, Object>>) result).size());
            }
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (RuntimeException ex) {
            LOGGER_PRODUCTCONTROLLER.info(" method searchProduct() throw exception");
            throw new DataNotFindException("");
        }
    }

    /** Get product multi unit response entity.
     * 获取商品多个单位规格
     * @param appver appver
     * @param id id
     * @return the response entity
     */
    @RequestMapping(value = "/ns/{appver}/product/unit/get/{id}", method = RequestMethod.POST)
    public ResponseEntity<Object> getProductMultiUnit(@PathVariable("appver") String appver, @PathVariable("id") Long id) {
        SearchInfo searchInfo = new SearchInfo();
        if (null == id) {
            LOGGER_PRODUCTCONTROLLER.info("parameter check failed!");
            throw new DataNotFindException(
                    messageSourceUtils.getMessage(
                            com.vogue.supplymall.common.constant.ConstantBean.GLOBAL_SYSTEM + "product", null));
        }

        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            searchInfo.setSyscodeid(info.getSyscodeid());
            LOGGER_PRODUCTCONTROLLER.info("登录人 ：" + info.getLoginId());
        } else {
            LOGGER_PRODUCTCONTROLLER.info("未登录");
        }

        searchInfo.setId(id);
        List<Map<String, Object>> result = productService.findProductUnit(searchInfo);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
